/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import com.constants.UserPermissions;
import com.domain.Event;
import com.domain.Profile;
import com.domain.User;

public class UserDAO extends BaseObjectDAO{

	
	public User getUserByUsername(String username) {
		
		User userToSave = new User();
		if((username!=null)){
			Criteria crit = this.getSession().createCriteria(User.class);
			crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			crit.add(Expression.eq("deleted",false));
			crit.add(Expression.eq("approved",true));
	       	crit.add(Expression.eq("username",username));
	       	List<User> results = crit.list();
	       	if(results.size()>0) userToSave = results.get(0);
	       	else return null;
		}
		return userToSave;
	}

	public User save(User user) {
		this.getSession().saveOrUpdate(user);
		this.getSession().flush();
		if((user.getKeyId()!=null)){
			return user;
		}
		else return null;
	}
	
	public User saveUser(Long keyId, String username, User createdBy, String email, User lastUpdateUser, 
			String password, UserPermissions userPermissions, Profile profile, boolean deleted, boolean approved){
		User userToSave = null;
		if((keyId!=null)&&(keyId>0)){
			userToSave = this.getUserByKeyId(keyId);
	       	if(userToSave!=null){
	       		if((userToSave.getKeyId()!=null)){
	       			Timestamp now = new Timestamp(System.currentTimeMillis());
	       			userToSave.setEmail(email);
	       			userToSave.setLastUpdateUser(lastUpdateUser);
	       			userToSave.setCreationDate(now);
	       			userToSave.setLastModDate(now);
	       			userToSave.setDeleted(deleted);
	       			userToSave.setUserPermissions(userPermissions);
	       			userToSave.setProfile(profile);
	       			userToSave.setApproved(approved);
	       			return this.save(userToSave);
	       		}
	       		else return null;
	       	}
	       	else return null;
		}
		else{
			userToSave = new User();
			Timestamp now = new Timestamp(System.currentTimeMillis());
			userToSave.setUsername(username);
			userToSave.setCreatedBy(createdBy);
			userToSave.setEmail(email);
			userToSave.setLastModDate(now);
			userToSave.setPassword(password);
			userToSave.setLastUpdateUser(lastUpdateUser);
			userToSave.setUserPermissions(userPermissions);
			userToSave.setDeleted(deleted);
			userToSave.setProfile(profile);
			userToSave.setApproved(approved);
			return this.save(userToSave);
		}
		
		
	}
	
	public Profile buildProfile(Profile profile, String firstName, String lastName, String address1, String address2,
			String city, String state, String zip, String secondaryEmail, String phone, String employer, 
			String positionDescription, Timestamp employmentStartDate, String education, String major, Timestamp graduationDate,
			String professionalOrganizations, String studentOrganizations, boolean publicViewable){
		
		Profile profileToBuild = profile;
		
		if(profileToBuild==null) profileToBuild = new Profile();
		 
		profileToBuild.setFirstName(firstName);
		profileToBuild.setLastName(lastName);
		profileToBuild.setAddress1(address1);
		profileToBuild.setAddress2(address2);
		profileToBuild.setCity(city);
		profileToBuild.setState(state);
		profileToBuild.setZip(zip);
		profileToBuild.setSecondaryEmail(secondaryEmail);
		profileToBuild.setPhone(phone);
		profileToBuild.setEmployer(employer);
		profileToBuild.setPositionDescription(positionDescription);
		profileToBuild.setEmploymentStartDate(employmentStartDate);
		profileToBuild.setEducation(education);
		profileToBuild.setMajor(major);
		profileToBuild.setGraduationDate(graduationDate);
		profileToBuild.setProfessionalOrganizations(professionalOrganizations);
		profileToBuild.setStudentOrganizations(studentOrganizations);
		profileToBuild.setPublicViewable(publicViewable);
		
		return profileToBuild;
	}
	
	public User getUserByKeyId(Long keyId){
		User userToSave = null;
		if((keyId!=null)&&(keyId>0)){
			Criteria crit = this.getSession().createCriteria(User.class);
			crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			crit.add(Expression.eq("deleted",false));
	       	crit.add(Expression.eq("keyId",keyId));
	       	List<User> results = crit.list();
	       	userToSave = results.get(0);
		}
		return userToSave;
	}
	
	public List<User> getAllUsers(){
		Criteria crit = this.getSession().createCriteria(User.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
       	List<User> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<User> getPendingUsers() {
		Criteria crit = this.getSession().createCriteria(User.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",false));
		crit.add(Expression.eq("deleted",false));
       	List<User> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<User> searchProfiles(String username, String firstName, String lastName, String major) {
		Criteria crit = this.getSession().createCriteria(User.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",true));
		crit.add(Expression.eq("deleted",false));
		if(username!=null&&username.length()>0)crit.add(Expression.ilike("username", username));
		crit.createCriteria("profile","boo");
		if(firstName!=null&&firstName.length()>0)crit.add(Expression.ilike("boo.firstName", firstName));
		if(lastName!=null&&lastName.length()>0)crit.add(Expression.ilike("boo.lastName", lastName));
		if(major!=null&&major.length()>0&&(!major.equals("Major")))crit.add(Expression.ilike("boo.major", major));
       	List<User> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<User> getAllApprovedUsers() {
		Criteria crit = this.getSession().createCriteria(User.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",true));
		crit.add(Expression.eq("deleted",false));
       	List<User> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<User> getAllEmailUsers() {
		Criteria crit = this.getSession().createCriteria(User.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",true));
		crit.add(Expression.eq("deleted",false));
		crit.add(Expression.eq("subscribedToEmails",true));
       	List<User> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

}
